/* Developed by Vinh, Nguyen Hong <mansofta@gmail.com>
 * Copyright HIENDAI SOFTWARE COMPANY 2004
 * http://www.hiendai.com.vn
 * contact@hiendai.com.vn
*/
using System;
using System.IO;
using System.Text;
using System.Xml;

namespace MarcXmlParser {

	public class CControlfields
	{	
	protected	XmlNode ptCurrentRootNode;		
	protected	XmlNodeList ptCurrentControlfieldList;
	
	public CControlfields(ref XmlNode ptRecordRootNode)
		{
			ptCurrentRootNode=ptRecordRootNode;
			ptCurrentControlfieldList=ptCurrentRootNode.SelectNodes("./*[local-name() ='controlfield']");  
		}
	public void Refresh()
		{
			ptCurrentControlfieldList=ptCurrentRootNode.SelectNodes("./*[local-name() ='controlfield']");  
		}
	public int Count
	{
		get
		{
			return ptCurrentControlfieldList.Count; 
		}	
	}
    public bool Add(CControlfield ptControlfield)
		{	
			String myTag=ptControlfield.Tag;
			this.Filter(myTag);			
			if(ptCurrentControlfieldList.Count==0) //avoid duplicated in controlfield
			{
				ptCurrentRootNode.AppendChild(ptCurrentRootNode.OwnerDocument.ImportNode(ptControlfield.RootNode,true));			  
				this.Refresh();
				return true;
			}			
			this.Refresh();
			return false;
		}
        public void Remove(int ControlfieldIndex)
		{		
			ptCurrentRootNode.RemoveChild(ptCurrentControlfieldList.Item(ControlfieldIndex));
		}
        public void Remove(ref CControlfield ptControlfield)
		{		
			ptCurrentRootNode.RemoveChild(ptControlfield.RootNode);
		}
	public void Filter(String sTag)
		{
	  	    StringBuilder QueryStr = new StringBuilder();
			QueryStr.Append("./*[local-name() ='controlfield'][@tag='");		
			QueryStr.Append(sTag);
			QueryStr.Append("']");
			ptCurrentControlfieldList=ptCurrentRootNode.SelectNodes(QueryStr.ToString());  
		}
    public bool get_Controlfield(String sTag, ref CControlfield ptControlfield)
		{								
			XmlNode ptXnode;
			StringBuilder QueryStr = new StringBuilder();
			QueryStr.Append("./*[local-name() ='controlfield'][@tag='");		
			QueryStr.Append(sTag);
			QueryStr.Append("']");
			ptXnode=ptCurrentRootNode.SelectSingleNode(QueryStr.ToString());
			if (ptXnode!=null)
			{
				ptControlfield.load_RootNode(ref ptXnode); 
				return true;
			}
			else
			{
				return false;				
			}			
		}

        public CControlfield Controlfield(int Index)
		{
			CControlfield ptControlfield;
			ptControlfield=new CControlfield();
            XmlNode myNode = ptCurrentControlfieldList.Item(Index);
            ptControlfield.load_RootNode(ref myNode); 
			return ptControlfield;				
		}
        public CControlfield Controlfield(String sTag)
		{
			CControlfield ptControlfield;
			ptControlfield=new CControlfield();			
			XmlNode ptXnode;
			StringBuilder QueryStr = new StringBuilder();
			QueryStr.Append("./*[local-name() ='controlfield'][@tag='");		
			QueryStr.Append(sTag);
			QueryStr.Append("']");
			ptXnode=ptCurrentRootNode.SelectSingleNode(QueryStr.ToString());
			if (ptXnode!=null)
			{
				ptControlfield.load_RootNode(ref ptXnode); 
			}
			return ptControlfield;				
		}
	};
}
